<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>YUI substitute Tests</title>
</head>

<body class="yui3-skin-sam">
<h1>substitute Tests</h1>
<p><input type="button" value="Run Tests" id="btnRun" disabled="true" /></p>

<script type="text/javascript" src="../../../build/yui/yui.js"></script>
<script type="text/javascript">

(function() {
    YUI({
        filter: "debug",
        logExclude: {Dom:true,
        Selector:true, Node:true, attribute:true, event:true, base:true, widget:true} }).use("dump", "test", "console", "event-custom", function(Y) {

        var button = Y.one('#btnRun'),
            Assert = Y.Assert,
            ObjectAssert = Y.ObjectAssert;

        // Set up the page
        button.set("disabled", false);
        Y.on("click", function() {
            Y.Test.Runner.run();
        }, button);

        var myConsole = new Y.Console().render();

        var testSubstitute= new Y.Test.Case({
            name: "Substitute tests",


        test_substitute: function() {

            ////////////////////////////////////////////////////////////////////////////////

            var param = {domain: 'valvion.com',
                         media: 'http://media.{domain}/', /* nested */
                         contextdomain: { context1: 'context{domain}', context2: 'yahoo.com' },  /* the value is an object, we will use a custom function to extract the correct data */
                         contextmedia: 'http://contextmedia.{contextdomain context1}/'};
            var url;

            // standard replace, nested
            url = Y.substitute("{media}logo.gif", param, null, true);
            Assert.areEqual(url, "http://media.valvion.com/logo.gif");


            // If the replacement value is an object, use the meta info as a key to extract the
            // correct data.  Otherwise just return the value.
            function multipleChoice(key, val, meta) {
                return (Y.Lang.isObject(val)) ? val[meta] : val;
            }

            // "random data" is not used since the value for the contextmedia key is a string.
            // contextdomain uses "context1" as a key to expand the string correctly
            url = Y.substitute("{contextmedia random data}logo.gif", param, multipleChoice, true);
            Assert.areEqual(url, "http://contextmedia.contextvalvion.com/logo.gif");

            ////////////////////////////////////////////////////////////////////////////////

            var obj = {
                level1_1: 1.1,
                level1_2: 1.2,
                level1_3: {
                    level2_1: 2.1,
                    level2_2: 2.2,
                    level2_3: {
                        level3_1: 3.1,
                        level3_2: 3.2,
                        level3_3: 3.3,
                        level3_4: 3.4
                    },
                    level2_4: 2.4
                },
                level1_4: 1.4
            };


            Assert.areEqual(
                Y.substitute("{testobj 0}", { testobj: obj }, null, true),
                "{level1_1 => 1.1, level1_2 => 1.2, level1_3 => {...}, level1_4 => 1.4}",
                "failed one level object dump"
            );

            Assert.areEqual(
                Y.substitute("{testobj 1}", { testobj: obj }, null, true),
                "{level1_1 => 1.1, level1_2 => 1.2, level1_3 => {level2_1 => 2.1, level2_2 => 2.2, level2_3 => {...}, level2_4 => 2.4}, level1_4 => 1.4}",
                "failed two level object dump"
            );

            Assert.areEqual(
                Y.substitute(
                    "{testobj 10}", { testobj: obj }, null, true),
                "{level1_1 => 1.1, level1_2 => 1.2, level1_3 => {level2_1 => 2.1, level2_2 => 2.2, level2_3 => {level3_1 => 3.1, level3_2 => 3.2, level3_3 => 3.3, level3_4 => 3.4}, level2_4 => 2.4}, level1_4 => 1.4}",
                "failed deep object dump"
            );

            var arr = [
                1.1,
                1.2,
                [
                    2.1,
                    2.2,
                    [
                        3.1,
                        3.2,
                        3.3,
                        3.4
                    ],
                    2.4
                ],
                1.4
            ];

            Assert.areEqual(
                Y.substitute("{testarr 1}", { testarr: arr }, null, true),
                "[1.1, 1.2, [2.1, 2.2, {...}, 2.4], 1.4]",
                "failed two level array dump"
            );

            var mix = [
                1.1,
                new Date(),
                {
                    level2_1: 2.1,
                    level2_2: 2.2,
                    level2_3: [
                        3.1,
                        3.2,
                        3.3,
                        3.4
                    ],
                    level2_4: 2.4
                },
                1.4,
                function(){}
            ];

            var result = Y.substitute("{testmix 1}", { testmix: mix }, null, true);

            Assert.isTrue(
                (result.indexOf("GMT" > -1)) ? true : false,
                "failed two level mixed object with a date (date should have produced an output with GMT in it)"
            );


            ////////////////////////////////////////////////////////////////////////////////

            var tostr = {
                custom1_1: 1.1,
                custom1_2: 1.2,
                custom1_3: {
                    custom2_1: 2.1,
                    custom2_2: 2.2,
                    custom2_3: {
                        custom3_1: 3.1,
                        custom3_2: 3.2,
                        custom3_3: 3.3,
                        custom3_4: 3.4
                    },
                    custom2_4: 2.4
                },
                custom1_4: 1.4,
                toString: function() {
                    return "custom toString executed";
                }
            };

            Assert.areEqual(
                Y.substitute("{customtostr 1}", { customtostr:  tostr }, null, true),
                "custom toString executed",
                "failed: custom toString should have been used"
            );

            // Assert.areEqual(
            //     Y.substitute("{customtostr dump 1}", { customtostr:  tostr }, null, true),
            //     "{custom1_1 => 1.1, custom1_2 => 1.2, custom1_3 => {custom2_1 => 2.1, custom2_2 => 2.2, custom2_3 => {...}, custom2_4 => 2.4}, custom1_4 => 1.4, toString => f(){...}}",
            //     "failed: custom toString should NOT have been used because the dump keyword should override it"
            // );

            ////////////////////////////////////////////////////////////////////////////////

            var template = 'My {prefix} name is {name}. {signature}';
            var obj = {
                prefix:'last',
                name: 'Zappa',
                signature: '{I am a replacement string }{}'
            };

            // testing {} in the replacement text
            Assert.areEqual("My last name is Zappa. {I am a replacement string }{}", Y.substitute(template, obj));

        }

        });


	Y.Test.Runner.setName("Substitute");
        Y.Test.Runner.add(testSubstitute);
        Y.Test.Runner.run();
    });
})();
</script>
</body>
</html>
